$(function(){
    
    var idRotina = $('#idRotina').val();
    jQuery("#gridExames").jqGrid({
        caption: "Exames",
        url: "intranet/rotina/exames/id/"+idRotina,
        datatype: "json",
        colNames:['Exame', 'Tipo Resultado', 'Metodologia', 'Kit Sorológico'],
        colModel:[
            {name:'nome', index:'nome', width:150, align:"center"},
            {name:'resultado', index: 'resultado', width: 100, align: 'center', search:false},
            {name:'metodologia', index:'metodologia', width:150, align:"center", search:false, editable:false},
            {name:'kitSorologia', index:'kitSorologia', width:150, align:"center", search:false, editable:false}
        ],
        //rowNum: 10,
        //rowList: [10,20,30,50,100],
        height: 100,
        pager: '#navExames',
        sortname: "idExame",
        sortorder: "ASC",
        multiselect: false,
        viewrecords: true,
        scroll:true,
        onSelectRow: function(ids) {
            if(ids == null) {
                ids=0;
		if(jQuery("#gridAmostras").jqGrid('getGridParam','records') >0 )
		{
                    jQuery("#gridAmostras").jqGrid('setGridParam',{url:"intranet/rotina/amostras/idRotina/"+idRotina+"/idExame/"+ids,page:1})
                    .trigger('reloadGrid');
		}
            } else {
                jQuery("#gridAmostras").jqGrid('setGridParam',{url:"intranet/rotina/amostras/idRotina/"+idRotina+"/idExame/"+ids,page:1})
		.trigger('reloadGrid');			
            }
	}

    })
    .navGrid("#navExame",{edit: false,add: false,del: false,search: false,view: false,refresh: false})
});

$(function(){
    var idRotina = $('#idRotina').val();
    jQuery("#gridAmostras").jqGrid({
        height: 100, 
        url: 'intranet/rotina/amostras/idRotina/'+idRotina+'/idExame/'+0,
        datatype: "json", 
        colNames: ['Amostra', 'Inicial','Resposta', 'Valor 1', 'Valor 2'],
        colModel: [
            {name: 'idAmostra', index: 'idAmostra', width: 110, align: 'center', search:false},
            {name: 'inicial', index: 'inicial', width: 90, align: 'center', search:false},
            {name: 'resposta', index: 'resposta', width: 105, align: 'center', search:false},
            {name: 'valor1', index: 'valor1', width: 120, align: 'center', search:false},
            {name: 'valor2', index: 'valor2', width: 120, align: 'center', search:false}
        ],
        pager: '#navAmostras', 
        sortname: 'idAmostra', 
        viewrecords: true,
        sortorder: "asc", 
        multiselect: false,
        scroll: true,
        caption:"Amostras" 
    }).navGrid('#navAmostras',{add:false,edit:false,del:false,search:false,view:false,refresh:false});
});




jQuery("#btnLiberar").click( function() {
    var responsavel = '';
    var senhaResp = ''; 
    var auxiliar = '';
    var senhaAux = '';
    responsavel = $('#loginBio').val();
    senhaResp = $('#senhaResp').val();
    auxiliar = $('#loginAux').val();
    senhaAux = $('#senhaAux').val();
    
    if((responsavel == '')||(senhaResp == '')||(auxiliar == '')||(senhaAux == '')){
        alert("Todos os campos devem ser preenchidos.")
    }
    else if(responsavel === auxiliar){
        alert('Informe usuários diferentes para liberar a rotina!')
    }
    
     jQuery.ajax({
            type: 'POST',
            url: 'intranet/rotina/autenticar/',
            data: jQuery("#frmUsuariovrfc").serialize(),
            dataType: 'json',
            async: false,
            
            success: function(json){
                
                if(json.status)
                {
                $.ajax({
                       type: 'POST',
                       url: 'intranet/rotina/librotina/',
                       data: jQuery("#frmUsuariovrfc").serialize(),
                       dataType: 'json',
                       async: false,
                       
                       success: function(json){
                           
                                if(json.status){
                                    alert("Operação realizada com sucesso");
                                    intranet.carregaModulo('intranet/Rotina/');
                                }else{
                                    alert("Não foi possível realizar a operação");
                                }            
                            }
                    });  
                }
            }
        });
    
    
            
     
});

jQuery("#btnCancelar").click( function() {
    if(confirm("Tem certeza que deseja  cancelar?")){
        intranet.carregaModulo('intranet/Rotina/');
    }    
});
